<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <div class="contain">
          <div class="el-tabs__nav-wrap">
            <span class="title">组织结构</span>
          </div>

          <!-- <hr> -->
          <div class="box">
            <!-- 用一个行列布局 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px"
              class="treeCon"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span style="font-weight: bolder;">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col style="padding-left: 50px;">负责人</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span style="padding-left: 40px;"> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          v-allow="'permiss-add'"
                          @click.native="add('')"
                        >添加一级部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <el-tree
              :default-expand-all="true"
              :data="data"
              :props="defaultProps"
              @node-click="handleNodeClick"
            >
              <template #default="{ data,node }">
                <el-row
                  type="flex"
                  justify="space-between"
                  align="middle"
                  style="height: 40px; width: 100%"
                >
                  <span v-if="data.children.length === 0" class="svg-container">
                    <svg-icon icon-class="user111" />
                  </span>
                  <el-col :span="20">
                    <span v-if="data.children.length !== 0" class="svg-cont">
                      <svg-icon :icon-class="node.expanded ? 'user333' : 'user222'" />
                      <!-- <svg-icon icon-class="user333" /> -->
                    </span>
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <!-- 两个内容 -->
                      <el-col style="padding-left: 65px;">{{ data.manager }}</el-col>
                      <el-col>
                        <!-- 下拉菜单 element -->
                        <el-dropdown>
                          <span style="padding-left: 55px;"> 操作<i class="el-icon-arrow-down" /> </span>
                          <!-- 下拉菜单 -->
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              v-allow="'permiss-add'"
                              @click.native="add(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              v-allow="'permiss-add'"
                              @click.native="addFn(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data.children.length === 0"
                              v-allow="'permiss-add'"
                              @click.native="del(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </div>
        </div>
      </el-card>
    </div>
    <el-dialog
      v-if="showDialog"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
    >
      <DeptDialog
        :id="curId"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="showDialog = false"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/tree.js'
import { tranListToTreeData } from '@/utils/index.js'
import DeptDialog from './deptDialog.vue'
export default {
  components: { DeptDialog },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      curId: '',
      isEdit: false,
      isShow: true
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res.data.depts)
      res.data.depts.shift()
      this.data = tranListToTreeData(res.data.depts)
    },
    add(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    addFn(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
    },
    del(id) {
      this.$confirm('你确定要删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    async doDel(id) {
      const res = await delDepartment(id)
      console.log(res)
      this.loadDepartments()
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    }
  }
}
</script>
<style scoped>

element.style {
}
b, strong {
    font-weight: bolder;
}
*, :after, :before {
    -webkit-box-sizing: inherit;
    box-sizing: inherit;
}
user agent stylesheet
strong {
    font-weight: bold;
}
style attribute {
    overflow: scroll;
    white-space: nowrap;
}
.el-card {
    border: 1px solid #ebeef5;
    background-color: #fff;
    color: #303133;
    -webkit-transition: .3s;
    transition: .3s;
}
body {
    height: 100%;
    -moz-osx-font-smoothing: grayscale;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-family: Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Arial,sans-serif;
    font-size: 14px;
    color: #555;
}
.el-card {
  padding: 20px;
}
.contain {
  padding-left: 20px;
  padding-top: 20px;
  text-align: left;
  margin: 0 0 15px;
}
.contain .title {
  display: inline-block;
  color: #409eff;
  font-size: 14px;
  line-height: 45px;
}
.el-tabs__nav-wrap::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 2px;
  background-color: #e4e7ed;
  z-index: 1;
}
.box{
      padding: 30px 120px 0;
}
.treeCon{
      border-bottom: 1px solid #cfcfcf;
    padding: 10px 0;
    margin-bottom: 10px;
}
.svg-container {
  padding: 6px 0px 6px 0px;
  color: #000;
  vertical-align: middle;
  font-size: 12px;
  width: 12px;
  display: inline-block;
}
.svg-cont {
  font-size: 12px;
  vertical-align: middle;
}
</style>
